//
//  PGEventItem.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-18.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "PGEventItem.h"
#import "EpointDateUtils.h"
#import "UIView+Addtions.h"
#import "UIPopoverListView.h"

#define PopoverListCellHeight 50.0f

typedef enum {
    NullEvent,
    DeleteEvent,
    EditEvent,
    ReadEvent
} EventStyle;

@interface PGEventItem () <UIPopoverListViewDataSource, UIPopoverListViewDelegate>

@property (nonatomic, strong) UIImageView *leftImage;
@property (nonatomic, strong) UILabel *Subject;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UIButton *contentView;

@end

@implementation PGEventItem {
    EventStyle eventStyle;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView = [UIButton buttonWithType:UIButtonTypeCustom];
        [_contentView setBackgroundImage:[UIImage imageNamed:@"cellBG"] forState:UIControlStateHighlighted];
        [_contentView addTarget:self action:@selector(contentViewTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        _contentView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self addSubview:_contentView];
        
        [self addLongPressEvent];

        self.leftImage = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"myday_ic_task"]];
        
        [_contentView addSubview:_leftImage];
        
        self.Subject = [[UILabel alloc] initWithFrame:CGRectZero];
        [_contentView addSubview:_Subject];
        
        self.time = [[UILabel alloc] initWithFrame:CGRectZero];
        [_contentView addSubview:_time];
        
        [self setDefaultValue];
    }
    return self;
}

- (void)setDefaultValue {
    _time.font = [UIFont systemFontOfSize:15];
    _time.textColor = [UIColor darkGrayColor];
}

- (void)setScheduleInfo:(ScheduleInfo *)scheduleInfo {
    _scheduleInfo = scheduleInfo;
    
    _Subject.text = scheduleInfo.Subject;
    _time.text = [EpointDateUtils convertStr2FromStr:scheduleInfo.FromDateTime oldFormat:@"yyyy-MM-dd HH:mm" withNewFormat:@"HH:mm"];
    UIImage *impImage = [UIImage imageNamed:[NSString stringWithFormat:@"imp_%@", _scheduleInfo.Importance]];
    _leftImage.image = impImage;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _leftImage.size = CGSizeMake(30, 30);
    _leftImage.left = (_contentView.height - _leftImage.height)*0.5;
    _leftImage.top = _leftImage.left;
    
    _time.size = CGSizeMake(43, 21);
    _time.right = _contentView.width-5;
    _time.centerY = _contentView.height*0.5;
    
    _Subject.size = CGSizeMake(_time.left - _leftImage.right - 10, 30);
//    _Subject.backgroundColor = [UIColor redColor];
    _Subject.left = _leftImage.right+5;
    _Subject.centerY = _contentView.height*0.5;
}

- (void)contentViewTouchUpInside {
    [_contentView setBackgroundImage:nil forState:UIControlStateNormal];
    [self editCurrentItem];
}

#pragma mark - long press

- (void)addLongPressEvent {
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    longPress.minimumPressDuration = 0.8;
    [_contentView addGestureRecognizer:longPress];
}

- (void)longPressed:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        [self showPoplistview];
    }
}

- (void)showPoplistview {
    CGFloat pHeight = TitleHeight + PopoverListCellHeight*2;
    CGFloat pTop = (AppHeight - SystemTakeHeight - BottomBarHeight - pHeight)*0.5;
    CGFloat pWidth = AppWidth - 60;
    UIPopoverListView *poplistview = [[UIPopoverListView alloc] initWithFrame:CGRectMake(30, pTop, pWidth, pHeight)];
    poplistview.delegate = self;
    poplistview.datasource = self;
    poplistview.listView.scrollEnabled = TRUE;
    [poplistview setTitle:@"操作"];
    [poplistview show];
}

#pragma mark - UIPopoverListViewDataSource

- (UITableViewCell *)popoverListView:(UIPopoverListView *)popoverListView
                    cellForIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:identifier];
    NSString *lbText;
    switch (indexPath.row) {
        case 0:
            lbText = @"删除";
            break;
        case 1:
            lbText = @"编辑";
            break;
        case 2:
            lbText = @"查看";
            break;
        default:
            break;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:18.0];
    cell.textLabel.text = lbText;
    return cell;
}

- (NSInteger)popoverListView:(UIPopoverListView *)popoverListView
       numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (void)deleteCurrentItem {
    if (_delegate && [_delegate respondsToSelector:@selector(deleteItem:)]) {
        [_delegate deleteItem:self];
    }
}

- (void)editCurrentItem {
    if (_delegate && [_delegate respondsToSelector:@selector(editItem:)]) {
        [_delegate editItem:self];
    }
}

#pragma mark - UIPopoverListViewDelegate

- (void)popoverListView:(UIPopoverListView *)popoverListView
     didSelectIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"indexpATH:%d", indexPath.row);
    switch (indexPath.row) {
        case 0:
            //lbText = @"删除";
            eventStyle = DeleteEvent;
            break;
        case 1:
            //lbText = @"编辑";
            eventStyle = EditEvent;
            break;
        case 2:
            //lbText = @"查看";
            eventStyle = ReadEvent;
            break;
        default:
            break;
    }
}

- (CGFloat)popoverListView:(UIPopoverListView *)popoverListView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return PopoverListCellHeight;
}

- (void)dismissFromParentView:(UIPopoverListView *)popoverListView {
    NSLog(@"dismiss");
    switch (eventStyle) {
        case DeleteEvent:
            [self deleteCurrentItem];
            break;
        case EditEvent:
            [self editCurrentItem];
            break;
        default:
            break;
    }
    eventStyle = NullEvent;
}

@end
